package com.zengdada.authority.controller;

import com.github.pagehelper.PageInfo;
import com.zengdada.authority.exception.RoutineException;
import com.zengdada.authority.model.DmlColumn;
import com.zengdada.authority.service.DmlColumnAuthorityService;
import com.zengdada.authority.utils.JsonMessage;
import com.zengdada.authority.utils.mapper.Page;
import com.zengdada.authority.utils.mapper.Sort;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

import static com.zengdada.authority.utils.JsonMessage.fail;
import static com.zengdada.authority.utils.JsonMessage.success;

/**
 * @Auther: 12614
 * @Contact: 1261438511@qq.com
 * @Date: 2019/3/7 18:44
 * @Description:
 */
@Controller
@RequestMapping("/authority/dml_column")
public class DmlColumnAuthorityController {

    @Autowired
    @Qualifier("dmlColumnAuthorityServiceImpl")
    DmlColumnAuthorityService dmlColumnAuthorityService;


    @GetMapping(value = "/list.html")
    public String listPage(HttpServletRequest request, @RequestParam("dmlId") String dmlId) {
        request.setAttribute("title", "DML_COLUMN 管理");
        if (StringUtils.isBlank(dmlId)) throw new RoutineException("异常请求，缺少dmlId参数");
        request.setAttribute("dmlId", dmlId);
        return "authority/dynamic/dml_column/list";
    }

    @PostMapping(value = "/list.do")
    @ResponseBody
    public JsonMessage list(@RequestParam(value = "searchs", required = false) String searchs, Page page, Sort sort) {
        try {
            PageInfo<Map<String, Object>> pageInfo = dmlColumnAuthorityService.selectDetailedPageInfoByParam(searchs, page, sort);
            return success(pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @GetMapping(value = "/edit.html")
    public String edit(HttpServletRequest request, String id, @RequestParam("dmlId") String dmlId) {
        if (StringUtils.isBlank(dmlId)) throw new RoutineException("异常请求，缺少dmlId参数");
        request.setAttribute("id", id);
        request.setAttribute("dmlId", dmlId);
        request.setAttribute("title", StringUtils.isBlank(id) ? "DML_COLUMN 新增" : "DML_COLUMN 编辑");
        return "authority/dynamic/dml_column/edit";
    }

    @PostMapping(value = "/save.do")
    @ResponseBody
    public JsonMessage save(DmlColumn dmlColumn) {
        try {
            if (dmlColumnAuthorityService.save(dmlColumn) > 0)
                return success();
            else throw new Exception("未修改任何数据（1.权限不足；2.非本应用数据；3.数据不存在）");
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }


    @RequestMapping(value = "/regenerate.do", method = RequestMethod.POST)
    @ResponseBody
    public JsonMessage regenerate(@RequestParam(value = "dmlId", defaultValue = "") String dmlId) {
        try {
            if ("".equals(dmlId)) throw new Exception("请求参数错误!");
            dmlColumnAuthorityService.autoGenerationColumn(dmlId, false);
            return success("重新生成列 成功", null);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @PostMapping(value = "/detail/{id}.do")
    @ResponseBody
    public JsonMessage detail(@PathVariable(value = "id") String id) {
        try {
            return success(dmlColumnAuthorityService.selectById(id));
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }


    @PostMapping(value = "/del.do")
    @ResponseBody
    public JsonMessage del(@RequestParam(value = "id") String id) {
        try {
            return success("删除了" + dmlColumnAuthorityService.batchDelete(id) + "条数据", null);
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }


    @PostMapping(value = "/column_types.do")
    @ResponseBody
    public JsonMessage column_types(@RequestParam(value = "dmlId") String dmlId) {
        try {
            return success(dmlColumnAuthorityService.getColumnTypes(dmlId));
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }


    @PostMapping(value = "/oracle_types.do")
    @ResponseBody
    public JsonMessage oracle_types() {
        try {
            return success(dmlColumnAuthorityService.getOracleTypes());
        } catch (Exception e) {
            return fail(e.getMessage());
        }
    }
}
